package com.remenote.remenoteback.service.impl;

import com.remenote.remenoteback.dao.CommentRepository;
import com.remenote.remenoteback.entity.doo.CommentDo;
import com.remenote.remenoteback.entity.resp.ServerResponse;
import com.remenote.remenoteback.entity.vo.CommentSectionVo;
import com.remenote.remenoteback.service.CommentService;
import com.remenote.remenoteback.util.ThreadLocalUtil;
import com.remenote.remenoteback.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: remenote-back
 * @BelongsPackage: com.remenote.remenoteback.service.impl
 * @Author: 23392
 * @CreateTime: 2024-03-16  20:48
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentRepository commentRepository;

    @Override
    public ServerResponse makeComment(String noteOwnerUserName, String notebookName, String noteTitle, String content) {
        CommentDo commentDo = new CommentDo();
        commentDo.setPublisher(ThreadLocalUtil.getUserName())
                .setUsername(noteOwnerUserName)
                .setNotebookName(notebookName)
                .setNoteTitle(noteTitle)
                .setCommentContent(content);
        commentRepository.save(commentDo);
        return ServerResponse.buildSuccessResponse();
    }

    @Override
    public ServerResponse<CommentSectionVo> viewComment(String notebookName, String noteTitle, String noteOwnerName) {
        List<CommentDo> commentDos = commentRepository.findCommentDosByUsernameAndNotebookNameAndNoteTitleAndIsDelete(
                noteOwnerName,
                notebookName,
                noteTitle,
                false);
        CommentSectionVo commentSectionVo = CommentSectionVo.fromCommentDoList(commentDos, notebookName, noteTitle);
        return ServerResponse.buildSuccessResponse(commentSectionVo);
    }

    @Override
    public ServerResponse deleteComment(String notebookName, String noteTitle, String createDate, String publisher) {
        CommentDo commentDo = commentRepository.findCommentDoByUsernameAndNotebookNameAndNoteTitleAndCreatedTimeAndPublisher(
                ThreadLocalUtil.getUserName(),
                notebookName,
                noteTitle,
                TimeUtil.parse(createDate),
                publisher
        );
        commentDo.setIsDelete(true);
        commentRepository.save(commentDo);
        return ServerResponse.buildSuccessResponse();
    }
}
